#include "SharedVariables.fxh"

sampler depthSampler = sampler_state
{
	Texture=(DepthRT);
	MAGFILTER = POINT;
	MINFILTER = POINT;
	MIPFILTER = POINT;
	AddressU = Clamp;
	AddressV = Clamp;
};

sampler diffuseSampler = sampler_state
{
	Texture=(DiffuseRT);
	MAGFILTER = POINT;
	MINFILTER = POINT;
	MIPFILTER = POINT;
	AddressU = Clamp;
	AddressV = Clamp;
};

sampler normalSampler = sampler_state
{
	Texture=(NormalRT);
	MAGFILTER = POINT;
	MINFILTER = POINT;
	MIPFILTER = POINT;
	AddressU = Clamp;
	AddressV = Clamp;
};

texture LightRT;
sampler lightSampler = sampler_state
{
	Texture=(LightRT);
	MAGFILTER = POINT;
	MINFILTER = POINT;
	MIPFILTER = POINT;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct DrawFinalVSInput
{
    float3 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

struct DrawFinalVSOutput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

DrawFinalVSOutput DrawFinalVS(DrawFinalVSInput input)
{
    DrawFinalVSOutput output;

    output.Position = float4(input.Position, 1);
    output.TexCoord = input.TexCoord - halfPixel;

    return output;
}

float4 DrawFinalPS(DrawFinalVSOutput input) : COLOR0
{
	float3 diffuseColor = tex2D(diffuseSampler, input.TexCoord).rgb;
	float4 lightData = tex2D(lightSampler, input.TexCoord);
	float3 lightColor = lightData.rgb;
	float specular = lightData.a;
	return float4(diffuseColor*lightColor + specular, 1);
}

float4 DrawFinalDebugPS(DrawFinalVSOutput input) : COLOR0
{
	float4 color;
	float2 coord;
	coord.x = (input.TexCoord.x * 2.0f) % 1.0f;
	coord.y = (input.TexCoord.y * 2.0f) % 1.0f;
	if (input.TexCoord.x < 0.5f && input.TexCoord.y < 0.5f) {
		color = tex2D(diffuseSampler, coord);
	}
	if (input.TexCoord.x < 0.5f && input.TexCoord.y >= 0.5f) {
		color = tex2D(normalSampler, coord);
	}
	if (input.TexCoord.x >= 0.5f && input.TexCoord.y < 0.5f) {
		color = tex2D(depthSampler, coord);
	}
	if (input.TexCoord.x >= 0.5f && input.TexCoord.y >= 0.5f) {
		color = tex2D(lightSampler, coord);
	}
	return color;
}

technique DrawFinal
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 DrawFinalVS();
        PixelShader = compile ps_2_0 DrawFinalPS();
    }
}

technique DrawFinalDebug
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 DrawFinalVS();
        PixelShader = compile ps_2_0 DrawFinalDebugPS();
    }
}